/**
 * Demo program for Range Encoding Compressor implementation.
 * See http://winterwell.com/software/compressor.php
 */

import java.util.Random;
import java.io.*;

class CompressorFile {

	static String mode;
	static String dir;
	static String inFile;
	static String outFile;
	
	static int inFileSize;
	static int outFileSize;
	
	static void LOG(String a)
    {
        System.out.println(a);
    }
	
	public static void main(String[] args) {
		try{
			if(exec(args))
			{
				LOG(">>>>> Done! <<<<<");
			}
			else{
				
			}
		}
		catch(Exception ex)
		{
			LOG("!!! Error !!!");
			LOG(ex.toString());
		}
		/*
		byte[] test = new byte[300000];
		Random myRND = new Random();
		for (int i = 0; i < test.length; i++) {
			test[i] = (byte) (myRND.nextInt() & 0x44);
		}

		byte[] compd = Compressor.compress(test);
		
		System.out.println("Compressed " + test.length + " bytes to " + compd.length + " bytes");
		System.out.println("Compression " + compd.length * 100/test.length + "%");
		
		byte[] result = Compressor.decompress(compd);
		
		if (result.length != test.length) {
			throw new RuntimeException("DECODE LENGTH ERROR");
		}
		for (int i = 0; i < test.length; i++) {
			if (test[i] != result[i]) {
				throw new RuntimeException("DECODE ERROR");
			}
		}
		System.out.println("Done!");
		*/
	}
	
	static boolean exec(String[] args) throws Exception
	{
		if(args.length == 0)
		{
			printHelp();
			return false;
		}
		// analyse argument
		for (int i=0; i<args.length; i++)
		{
			if ((args[i].compareToIgnoreCase("-m") == 0) && (i+1<args.length)) {
				mode = args[i+1].toLowerCase();
				if(mode.compareTo("e") !=0 && mode.compareTo("d") != 0)
				{
					printHelp();
					return false;
				}
				i++;

			} else if ((args[i].compareToIgnoreCase("-d") == 0) && (i+1<args.length)) {
				dir = args[i+1];
				i++;

			} else if ((args[i].compareToIgnoreCase("-i") == 0) && (i+1<args.length)) {
				inFile = args[i+1];
				i++;
			} else if ((args[i].compareToIgnoreCase("-o") == 0) && (i+1<args.length)) {
				outFile = args[i+1];
				i++;
			}
		}
		
		FileInputStream f = new FileInputStream(dir+"\\"+inFile);
		int size = f.available();
		inFileSize = size;
		byte[] data = new byte[size];
		f.read(data);
		// byte[] result;
		File file = new File(dir+"\\"+outFile);
		FileOutputStream out = new FileOutputStream(file);
		
		if(mode.compareTo("e") == 0)
		{
			byte[] result = Compressor.compress(data);
			outFileSize = result.length;
			out.write(result);
			LOG("Compression " + outFileSize * 100/inFileSize + "%");
		}
		else if(mode.compareTo("d") == 0)
		{
			byte[] result = Compressor.decompress(data);
			outFileSize = result.length;
			out.write(result);
			LOG("Decompression " + outFileSize * 100/inFileSize + "%");
		}
		// outFileSize = result.length;
		
		out.close();
		f.close();
		return true;
	}
	static void printHelp()
	{
		LOG("usage:");
		LOG("    java CompressorFile -m <e|d> -d <dir> -i <inFile> -o <outFile>");
		LOG("      e|d    	 : e = encode file, d = decode file");
		LOG("      dir       : directory for the compressor file");
		LOG("      inFile    : the file to compress/decompress)");
		LOG("      outFile   : result file.");
	}
}